﻿using System;
using System.IO;
using WatiNCF;
using WatiNCF.Extensions;
using WatiN.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core.Interfaces;

namespace WatiNCF.UnitTests
{
    [TestClass]
    public abstract class BaseWatinTest<T> : IWatinTest
        where T : class, IHomePageController
    {
        private TestContext testContextInstance;
        private int _timeOut = 15;
        private T _homePage;

        static BaseWatinTest()
        {
            IE.Settings.MakeNewIeInstanceVisible = true;
        }

        public T HomePage
        {
            get
            {
                if (_homePage == null)
                {
                    _homePage = Activator.CreateInstance<T>();
                }
                return _homePage;
            }
        }

        public string HomePageUrl
        {
            get { return HomePage.HomePageUrl;  }   
        }

        public IBrowser Browser
        {
            get { return ((IHomePageController)HomePage).Browser; }
            set { ((IHomePageController)HomePage).Browser = value; }
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [ClassInitialize]
        public virtual void ClassInitialize()
        {
        }

        [ClassCleanup]
        public virtual void ClassCleanUp()
        {
        }


        [TestInitialize()]
        public virtual void TestInitialize()
        {
            ((IHomePageController)HomePage).NavigateToHomePage();
            Browser.Refresh(); // Avoid cached data and start from clean slate
        }

        [TestCleanup()]
        public virtual void TearDown()
        {
            if (testContextInstance.CurrentTestOutcome == UnitTestOutcome.Failed)
            {
                string screenCaptureFileName = Path.Combine(TestContext.TestLogsDir, string.Format("{0}.jpg", TestContext.TestName));
                Browser.CaptureWebPageToFile(screenCaptureFileName);
                TestContext.WriteLine(screenCaptureFileName);
            }
            
            ((IHomePageController)HomePage).Browser.Close();
            ((IDisposable)HomePage).Dispose();
        }

        public virtual void SetShortTimeOut()
        {
            StoreTimeOut();
            IE.Settings.WaitForCompleteTimeOut = 5;
            IE.Settings.WaitUntilExistsTimeOut = 5;
            IE.Settings.AttachToIETimeOut = 5;
        }

        public virtual void SetMediumTimeOut()
        {
            StoreTimeOut();
            IE.Settings.WaitForCompleteTimeOut = 15;
            IE.Settings.WaitUntilExistsTimeOut = 15;
            IE.Settings.AttachToIETimeOut = 15;
        }

        public virtual void SetDefaultTimeOut()
        {
            StoreTimeOut();
            IE.Settings.WaitForCompleteTimeOut = 30;
            IE.Settings.WaitUntilExistsTimeOut = 30;
            IE.Settings.AttachToIETimeOut = 30;
        }

        public virtual void SetLongTimeOut()
        {
            StoreTimeOut();
            IE.Settings.WaitForCompleteTimeOut = 60;
            IE.Settings.WaitUntilExistsTimeOut = 60;
            IE.Settings.AttachToIETimeOut = 60;
        }

        private void StoreTimeOut()
        {
            _timeOut = IE.Settings.WaitForCompleteTimeOut;
        }

        public virtual void ResetTimeOut()
        {
            IE.Settings.WaitForCompleteTimeOut = _timeOut;
            IE.Settings.WaitUntilExistsTimeOut = _timeOut;
            IE.Settings.AttachToIETimeOut = _timeOut;
        }
    }
}